<?php
/**
 * Created by xuan.
 * Author: xuan
 * Date: 2018/8/3
 * Time: 18:24
 */

namespace app\api\model;
use think\Db;

class Address extends BaseModel
{	
	protected $table = 'address';
	
	public static function selectOneAddressByID($id){
		$data = model("Address")->where(array("id"=>$id))->find();
		return $data;
	}
	
	public static function selectOneAddressBydefault($mid){
		$data = model("Address")->where(array("mid"=>$mid,"ifdefault"=>1))->find();
		return $data;
	}
	
	public static function selectOneAddressByIdMid($all){
		$data = model("Address")->where(array("id"=>$all['id'],"mid"=>$all['mid']))->find();
		return $data;
	}
	
	
	public static function addressListsByMid($mid,$type = 1){
		$data = model("Address")->where(array("mid"=>$mid,'type'=>$type))->order('id','desc')->select();
		return $data;
	}
	
	public static function defaultAddressByMid($mid,$type = 1){
	    $data = model("Address")->where(array("mid"=>$mid,'type'=>$type,'ifdefault'=>1))->find();
	    return $data;
	}
	
	public static function addressListsByMidIfde($mid,$type=1){
	    $data = model("Address")->where(array("mid"=>$mid,"ifdefault"=>1,'type'=>$type))->order('id','desc')->find();
		return $data;
	}
	
	public static function addOneAddress($all)
    {
		$data = Address::create([
			'mid' => $all['mid'],
			'name' => $all['name'],
		    'type' => isset($all['type']) ? $all['type'] : 1,
			'tel' => $all['tel'],
			'addrvalue' => $all['addrvalue'],
			'addr' => $all['addr'],
			'addtime' => date('Y-m-d H:i:s',time()),
			'updatetime' => date('Y-m-d H:i:s',time())
		]);
		  
		return $data;
    }
	
	public static function updOneAddress($all)
    {
		$data=Db::name('Address')-> where('id',$all['id'])->update([
			'name' => $all['name'],
			'tel' => $all['tel'],
			'addrvalue' => $all['addrvalue'],
			'addr' => $all['addr'],
			'updatetime' => date('Y-m-d H:i:s',time())
		]);
		return $data;
    }
	
	public static function delOneAddress($id)
    {
		$data=Db::name('Address')-> where('id',$id)->delete();
		return $data;
    }
	
	public static function updOneAddressDef($all)
    {
		$data2=Db::name('Address')-> where('mid',$all['mid'])->where('type',$all['type'])->update(['ifdefault' => 2,'updatetime' => date('Y-m-d H:i:s',time())]);
		$data=Db::name('Address')-> where(array("id"=>$all['id'],"mid"=>$all['mid']))->update(['ifdefault' => 1,'updatetime' => date('Y-m-d H:i:s',time())]);
		return $data;
    }
}